﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Gauges.Interfaces;
using DcsGauges.Shared.DataStructures;
using Microsoft.Xna.Framework.Graphics;
using DcsGauges.Gui.Rendering;
using DcsGauges.DataStructures;
using Microsoft.Xna.Framework;
using DcsGauges.Gui.Gauges.ValueConverters;

namespace DcsGauges.Gui.Gauges.Generic
{
    class ToggleStateGauge : GuiRenderableBase, IValueGauge
    {
        #region Fields & Properties

        private DcsProperty                                     property                    = null;
        private Texture2D                                       trueTexture                 = null;
        private Texture2D                                       falseTexture                = null;
        private BoolConverter                                   valueConverter              = null;

        #endregion


        #region Constructors

        public ToggleStateGauge(GuiRenderableBase parent, URect area, DcsProperty property,
            Texture2D trueTexture, 
            Texture2D falseTexture, 
            BoolConverter converter) : base(parent, area)
        {
            this.property                       = property;
            this.trueTexture                    = trueTexture;
            this.falseTexture                   = falseTexture;
            this.valueConverter                 = converter;
        }

        #endregion


        #region Interface Implementation

        public Shared.DataStructures.DcsProperty DataProperty
        {
            get { return this.property; }
        }

        public float Value
        {
            get; set; 
        }

        #endregion


        #region Overrides

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Rectangle parentRect, float globalZIndex)
        {
            bool state                              = this.valueConverter.Convert(this.Value);
            Texture2D texture                       = state ? this.trueTexture : this.falseTexture;

            Rectangle? originalDimesions            = this.keepAspectRatio ? texture.Bounds : new Rectangle?();
            Rectangle viewportRect                  = this.CalcViewportRect(parentRect, originalDimesions);

            spriteBatch.Draw(texture, viewportRect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, base.NextFrontIndex(globalZIndex, 1));
        }

        #endregion
    }
}
